<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\datacache;

use think\facade\Cache;
use think\facade\Debug;
/**
 * 商品推荐
 * @package app\user_center\model
 */
class ShopGoods{
	public function getIndexGoodsRecommend($uid, $store_id, $warehouse_id, $num = 10){
		$cacheName = 'get_goods_recommend_uid'. $uid . $store_id;
		$recommend_list = $this -> getGoodsRecommend($uid, $store_id, $warehouse_id); //推荐列表

		$list = [];
		if(count($recommend_list) < $num){
			foreach($recommend_list as $key => $value){
				$list[] = $value;
				unset($recommend_list[$key]);
			}
		}else{
			$recommend_key = array_rand_tt($recommend_list, $num);
			foreach($recommend_key as $keys){
				$list[] = $recommend_list[$keys];
				unset($recommend_list[$keys]);
			}
		}
		//缓存池子 重新赋值
        Cache::set($cacheName, $recommend_list, 60*60);
        return array_out(200, 'success', $list);
	}
	
    /**
     * 首页 - 推荐商品 / 个人中心 - 为您推荐
     * @param uid 		int 用户id
     * @param store_id 	int 门店id
     * @param limit 获取池子大小 默认1000条
     * @param cache 是否更新 缓存 
     * @author shiqiang
     * @return array
     */
    public function getGoodsRecommend($uid, $store_id, $warehouse_id, $limit = 1000, $cache = false){
        $cacheName = 'get_goods_recommend_uid'.$uid . $store_id;
        if(!Cache::get($cacheName) || $cache){
			// 门店下的所有商品
			$map = [];
			$map[] = ['warehouse_id', 'in', $warehouse_id];
			$stock_goods = model('ErpGoodsWarehouseStockNum') -> group('goods_id') -> where($map) -> column('goods_id');
			if(!empty($stock_goods)){
                Cache::set($cacheName, $stock_goods, 60*60);//默认缓存1小时
            }
		}

		$list = Cache::get($cacheName);
        return (array)$list;
    }

}
